package com.xunua.mymycsjsdktest.PermissionMessage;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PermissionInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.xunua.mymycsjsdktest.R;
import com.xunua.mymycsjsdktest.SplashActivity;

import java.util.ArrayList;
import java.util.List;

public class PermissionsInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_agreement,tv_unagree;
    private RecyclerView recyclerView;
    private Button btn_agree;

    private String agreementStr="请您在使用PP助手前仔细阅读并同意《用户许可协议》和《隐私政策说明》";
    private List<PermissionsBean> mdata = new ArrayList<>();
    SPUtils agreement = SPUtils.getInstance("agreement", MODE_PRIVATE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissions_info);
        boolean isagree = agreement.getBoolean("isagree", false);
        if (isagree){
            startActivity(new Intent(PermissionsInfoActivity.this,SplashActivity.class));
            finish();
        }
        initview();
    }

    /**
     * 传入所需的权限名字和信息，和icon的本地资源位置
     */
    private void recyclerViewsetData() {
        mdata.add(new PermissionsBean(R.mipmap.phone,"电话权限","我们将用你的电话权限来这个这个"));
        mdata.add(new PermissionsBean(R.mipmap.save,"存储权限","我们将用你的存储权限来保存本地资源"));
        mdata.add(new PermissionsBean(R.mipmap.camera,"相机权限","我们将用你的相机权限来进行拍摄"));
        recyclerView.setAdapter(new MyAdapter());
    }

    private void initview() {
        tv_agreement = findViewById(R.id.Permissionsinfo_tv_agreement);
        SpannableStringBuilder spannable = new SpannableStringBuilder(agreementStr);
        spannable.setSpan(new TextClick(true),17,25,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new TextClick(false),26,34,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_agreement.setMovementMethod(LinkMovementMethod.getInstance());
        tv_agreement.setText(spannable);
        recyclerView=findViewById(R.id.Permissionsinfo_recyclerview);
        recyclerViewsetData();
        btn_agree=findViewById(R.id.Permissionsinfo_btn_agree);
        tv_unagree=findViewById(R.id.Permissionsinfo_tv_unagree);
        btn_agree.setOnClickListener(this);
        tv_unagree.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.Permissionsinfo_btn_agree:
                //同意授权
                startActivity(new Intent(PermissionsInfoActivity.this, SplashActivity.class));
                agreement.put("isagree",true);
                finish();
                break;
            case R.id.Permissionsinfo_tv_unagree:
                //拒绝授权
                Uri packageUri = Uri.parse("package:" + getPackageName());
                Intent i = new Intent(Intent.ACTION_DELETE, packageUri);
                startActivity(i);
//                System.exit(0);
                break;
        }
    }


    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(PermissionsInfoActivity.this).inflate(R.layout.item_permissions_info_message_onlyline, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            PermissionsBean bean = mdata.get(position);
            Glide.with(PermissionsInfoActivity.this).load(bean.getIcon()).into(holder.img);
            holder.tv_name.setText(bean.getName());
            holder.tv_message.setText(bean.getMessage());
        }

        @Override
        public int getItemCount() {
            return mdata.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder{
            private ImageView img;
            private TextView tv_name,tv_message;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                img=itemView.findViewById(R.id.permission_img);
                tv_name=itemView.findViewById(R.id.permission_name);
                tv_message=itemView.findViewById(R.id.permission_message);
            }
        }
    }

    private class PermissionsBean{
        private int icon;
        private String name;
        private String message;

        public PermissionsBean(int icon, String name, String message) {
            this.icon = icon;
            this.name = name;
            this.message = message;
        }

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    /**** 部分文字点击事件*/
    private class TextClick extends ClickableSpan {
        private boolean isUser_license;//true 则是用户许可协议，false则是隐私政策说明

        public TextClick(boolean isUser_license) {
            this.isUser_license = isUser_license;
        }

        @Override
        public void onClick(View view) {
            //你的业务需求，比如点击跳转到h5页面
            if (isUser_license) {
                //点击了用户许可协议
            }else {
                //点击了隐私政策说明
            }
            Intent intent = new Intent(PermissionsInfoActivity.this, WebviewActivity.class);
            intent.putExtra("url",isUser_license?"file:///android_asset/User_License_Agreement.html":"file:///android_asset/Privacy_policy_statement.html");
            startActivity(intent);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(Color.parseColor("#00BFFF"));   ////设置字体颜色
            ds.setUnderlineText(false); //设置没有下划线
        }
    }

}